from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.hashers import make_password, check_password
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    """"用户表"""
    GENDER_CHOICES = [
        ('M', 'Male'),
        ('F', 'Female'),
        ('O', 'Other'),
    ]

    age = models.IntegerField(
        blank=True,
        null=True,
        verbose_name='年龄',
    )
    gender = models.CharField(
        max_length=1,
        choices=GENDER_CHOICES,
        blank=True,
        verbose_name='性别',
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='注册时间',
    )
    avatar = models.ImageField(
        upload_to='avatars/',
        default='avatars/default.png',
        verbose_name='头像',
    )
    birthday = models.DateField(
        blank=True,
        null=True,
        verbose_name="生日",
    )
    phone = models.CharField(
        blank=True,
        null=True,
        max_length=20,
        verbose_name="电话号码",
    )
    mbti = models.CharField(
        blank=True,
        null=True,
        max_length=4,
        verbose_name="MBTI"
    )

    # 指定用于认证的用户名字段（默认就是username，可省略）
    USERNAME_FIELD = 'username'

    # 创建超级用户时需要填写的字段（默认包含email）
    REQUIRED_FIELDS = ['email']  # 根据你的需求调整

    def __str__(self):
        return self.username


class MentalHealth(models.Model):
    """"心理指数表"""
    user = models.ForeignKey(
        to = 'User',
        to_field = 'id',
        on_delete = models.CASCADE,
        related_name = 'mental_records',
        verbose_name = '用户'
    )
    date = models.DateField(auto_now_add=True, verbose_name='记录日期')
    score = models.IntegerField(verbose_name='心理指数')

    class Meta:
        verbose_name = '心理健康记录'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'date')  # 每人每天只能有一条记录
        ordering = ['-date']

    def __str__(self):
        return f"{self.user} - {self.date}: {self.score}"

class TestScore(models.Model):
    user = models.ForeignKey(
        to='User',
        to_field='id',
        on_delete=models.CASCADE,
        related_name='%(class)s_scores',
        verbose_name='用户'
    )
    date = models.DateField(auto_now_add=True, verbose_name='记录日期')
    score = models.FloatField(verbose_name='分数')

    class Meta:
        abstract = True
        ordering = ['-date']

    def __str__(self):
        return f"{self.user} - {self.date}: {self.score}"

class HCL32(TestScore):
    class Meta:
        verbose_name = 'HCL-32测试分数'
        verbose_name_plural = verbose_name

class SAS(TestScore):
    class Meta:
        verbose_name = 'SAS测试分数'
        verbose_name_plural = verbose_name

class SCL90(TestScore):
    class Meta:
        verbose_name = 'SCL-90测试分数'
        verbose_name_plural = verbose_name

class SDS(TestScore):
    class Meta:
        verbose_name = 'SDS测试分数'
        verbose_name_plural = verbose_name

class TreeHoleEntry(models.Model):
    MOOD_CHOICES = [
        ('happy', '😊 开心'),
        ('sad', '😢 难过'),
        ('angry', '😠 愤怒'),
        ('confused', '😖 困惑'),
        ('love', '❤️ 爱'),
    ]
    user = models.ForeignKey(
        to='User',
        to_field='id',
        on_delete=models.CASCADE,
        related_name='心里话',
        verbose_name='用户'
    )
    mood = models.CharField(
        max_length=10,
        choices=MOOD_CHOICES,
        default='happy',
        verbose_name="心情"
    )
    content = models.TextField(max_length=500, verbose_name="内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "树洞条目"
        verbose_name_plural = "树洞条目"
        ordering = ['-created_at']  # 默认按时间倒序排列

    def __str__(self):
        return f"{self.user.username}的树洞记录（{self.created_at}）"